package com.coollang.smartbra.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.coollang.smartbra.R;
import com.coollang.smartbra.activity.NewActionActivity;
import com.coollang.smartbra.base.MyApplication;
import com.coollang.smartbra.base.NewActionFragment;
import com.coollang.smartbra.beans.NewActionData;
import com.coollang.smartbra.constant.Constants;
import com.coollang.smartbra.presenter.NewActionCountFragmentPresenter;
import com.coollang.smartbra.utils.ActionTypeParse;
import com.coollang.smartbra.utils.ActivitySwitcher;
import com.coollang.smartbra.utils.UIUtils;
import com.coollang.smartbra.views.CircularSeekBarBcg;
import com.coollang.smartbra.views.CountView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author: Timmy
 * @data: 2017/12/14
 * @Fuction:
 */

public class NewActionCountFragment extends NewActionFragment {

    @BindView(R.id.tv_next)
    TextView mTvNext;
    @BindView(R.id.btn_start)
    Button mBtnStart;
    @BindView(R.id.circle_seekbar)
    CircularSeekBarBcg mCircularSeekBar;
    @BindView(R.id.nub_stop)
    TextView mNubStop;
    @BindView(R.id.tv_lock)
    TextView tvLock;
    @BindView(R.id.tv_current_nub)
    TextView mTvCurrentNub;
    @BindView(R.id.tv_heart_rate)
    CountView tvHeartRate;
    @BindView(R.id.tv_u_money)
    CountView tvUMoney;
    @BindView(R.id.pb_progressbar)
    ProgressBar mProgressBar;
    @BindView(R.id.tv_action_count_title)
    TextView mTvActionCountTile;
    @BindView(R.id.action_corrent_count)
    TextView mTvActionCorrentTile;
    @BindView(R.id.action_error_count)
    TextView mTvActionErrorTile;
    @BindView(R.id.tv_today_umoney)
    TextView mTvTodayUmoney;

    private int mActionType;
    private Unbinder mUnbinder;
    private NewActionCountFragmentPresenter mPresenter;
    private Context mContext;

    public static final int PROGRESSBAR_MAX = 1000;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

    @Override
    protected View inflateView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_actioncount, null, false);
        Bundle bundle = getArguments();
        mActionType = bundle.getInt(Constants.TYPE);
        return view;
    }

    @Override
    protected void initView(View view) {
        mUnbinder = ButterKnife.bind(this, view);
        mCircularSeekBar.setMaxProgress(PROGRESSBAR_MAX);
        mCircularSeekBar.setVisibility(View.INVISIBLE);

        // 长按结束
        initLongClick();
    }

    @Override
    protected void initData() {
        mPresenter = new NewActionCountFragmentPresenter(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mUnbinder.unbind();
        mPresenter.onDestroyView();
    }

    public int getActionType() {
        return mActionType;
    }

    @Override
    public void actionStart() {
        mBtnStart.setVisibility(View.GONE);
        mCircularSeekBar.setVisibility(View.VISIBLE);
        mNubStop.setVisibility(View.VISIBLE);
        mTvActionCountTile.setVisibility(View.GONE);
        mTvActionCorrentTile.setVisibility(View.VISIBLE);
        mTvActionErrorTile.setVisibility(View.VISIBLE);
        mTvTodayUmoney.setText("本次运动U币");
        tvUMoney.setText("0");
        tvUMoney.setTextSize(23);
    }

    @Override
    public void realCountData(int heartRate, int errorFlag, int errorType) {
        setHeartRate(heartRate);
        if (errorFlag != 0) {
            mPresenter.processCountData(heartRate, errorFlag, errorType);
        }
    }

    @Override
    public void actionFinish() {

    }

    @OnClick({R.id.btn_start, R.id.nub_stop, R.id.tv_action_details})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_start:
                ActivitySwitcher.goCountTimeAct(getActivity(), mBtnStart);
                break;
            case R.id.tv_action_details:
                ArrayList<NewActionData> actionDetails = mPresenter.getActionDetails();
                if (actionDetails.size() == 0) {
                    showT("还未开始运动");
                } else {
                    ActivitySwitcher.gotoNewCountActionDetails(getActivity(), actionDetails);
                }
                break;
            default:
                break;
        }
    }

    private void initLongClick() {
        mNubStop.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        nubStopPressed(R.drawable.shape_yellow_cbg, ContextCompat.getColor(mContext, R.color.white));
                        mPresenter.startCount();
                        break;
                    case MotionEvent.ACTION_UP:
                        mPresenter.removeHandler();
                        int progress = mPresenter.getProgress();
                        if (progress >= PROGRESSBAR_MAX) {
                            setCircularSeekBarProgress(progress);
                            // 获取正确动作个数
                            ((NewActionActivity) mContext).finishAction(mPresenter.getActionDetails());
                        } else {
                            nubStopPressed(R.drawable.shape_cbg, ContextCompat.getColor(mContext, R.color.progrss_bg));
                        }
                        mPresenter.setProgress(0);
                        setCircularSeekBarProgress(0);
                        break;
                    default:
                        break;
                }
                return true;
            }
        });
    }

    private void nubStopPressed(int background, int color) {
        mNubStop.setBackgroundResource(background);
        mNubStop.setTextColor(color);
    }

    public void setCircularSeekBarProgress(int progress) {
        mCircularSeekBar.setProgress(progress);
        mCircularSeekBar.invalidate();
    }

    private void showT(String message) {
        Toast.makeText(mContext, message, Toast.LENGTH_SHORT).show();
    }

    private void setHeartRate(int heartRate) {
        // 心率
        if (Integer.parseInt(MyApplication.mLogInDataBean.getIsalarm()) == 1) {
            if (heartRate >= Integer.parseInt(MyApplication.mLogInDataBean.getHr_alarm())) {
                tvHeartRate.startAnimation(AnimationUtils.loadAnimation(mContext, R.anim.anim_alpha));
                tvHeartRate.setTextColor(UIUtils.getColor(R.color.barrey_20));
            } else {
                tvHeartRate.clearAnimation();
                tvHeartRate.setTextColor(UIUtils.getColor(R.color.black));
            }
        }

        // 设置心率
        tvHeartRate.setText(String.valueOf(heartRate));
        tvHeartRate.setTextSize(23);
    }

    public void setCorrectCount(int correctCount) {
        int actionCode = Constants.ACTION_NAME_ID1[mActionType];
        tvUMoney.setText(String.valueOf(correctCount * ActionTypeParse.getUMoneyCount(actionCode)));
        mTvCurrentNub.setText(String.valueOf(correctCount));
    }

    /**
     * 设置progressbar
     */
    public void setProgress(int progress) {
        mProgressBar.setProgress(progress);
        // 设置二级
        mProgressBar.setSecondaryProgress(100);
        mProgressBar.setBackgroundColor(0x00000000);

        mTvActionCorrentTile.setText(progress + "%");
        mTvActionErrorTile.setText((100 - progress) + "%");
    }
}
